package com.example.demo.util;


import java.io.*;

public class FileUtil {
    // 读取文件返回字符串
    public static String readFile(String filePath) {
        try (FileReader fileReader = new FileReader(filePath);
            BufferedReader bufferedReader = new BufferedReader(fileReader)){
            StringBuffer result = new StringBuffer();
            while((true)) {
                String line = bufferedReader.readLine();
                if(line == null){
                    break;
                }
                result.append(line);
            }
            return result.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 将字符串写入文件
    public static void writeFile(String filePath, String content) {
        try(FileWriter fileWriter = new FileWriter(filePath)) {
            fileWriter.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
